import request from "@/utils/request";
import type { Product } from "@/types";

interface PaginationDto {
  page: number;
  limit: number;
}

interface PaginatedResponseDto<T> {
  items: T[];
  total: number;
  totalPages: number;
  currentPage: number;
  limit: number;
}

// 获取所有产品
export async function getProducts() {
  return (
    await request<Product[]>({
      url: "/products",
      method: "get",
    })
  ).data;
}

// 获取分页产品
export async function getPaginatedProducts(params: PaginationDto) {
  return await request<PaginatedResponseDto<Product>>({
    url: "/products/paginated",
    method: "get",
    params,
  });
}

// 根据ID获取单个产品
export async function getProductById(id: number) {
  return (
    await request<Product>({
      url: `/products/${id}`,
      method: "get",
    })
  ).data;
}

// 创建新产品
export async function createProduct(data: Partial<Product>) {
  return (
    await request<Product>({
      url: "/products",
      method: "post",
      data,
    })
  ).data;
}

// 获取推荐产品
export async function getRecommendedProducts() {
  return (
    await request<Product[]>({
      url: "/products/recommended",
      method: "get",
    })
  ).data;
}

// 获取分页推荐产品
export async function getRecommendedPaginatedProducts(params: PaginationDto) {
  return (
    await request<PaginatedResponseDto<Product>>({
      url: "/products/recommended/paginated",
      method: "get",
      params,
    })
  ).data;
}

// 删除产品
export async function deleteProduct(id?: number) {
  return request({
    url: `/products/${id}`,
    method: "delete",
  });
}

// 更新产品
export async function updateProduct(id: number, data: Partial<Product>) {
  return (
    await request<Product>({
      url: `/products/${id}`,
      method: "patch",
      data,
    })
  ).data;
}
